class ThoiKhoaBieusController < ApplicationController
  # GET /thoi_khoa_bieus
  # GET /thoi_khoa_bieus.json
  def index
    @thoi_khoa_bieus = ThoiKhoaBieu.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @thoi_khoa_bieus }
      format.csv { send_data ThoiKhoaBieu.to_csv(@search) }
      format.xls
    end
  end

  # GET /thoi_khoa_bieus/1
  # GET /thoi_khoa_bieus/1.json
  def show
    @thoi_khoa_bieu = ThoiKhoaBieu.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @thoi_khoa_bieu }
    end
  end

  # GET /thoi_khoa_bieus/new
  # GET /thoi_khoa_bieus/new.json
  def new
    @thoi_khoa_bieu = ThoiKhoaBieu.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @thoi_khoa_bieu }
    end
  end

  # GET /thoi_khoa_bieus/1/edit
  def edit
    @thoi_khoa_bieu = ThoiKhoaBieu.find(params[:id])
  end

  # POST /thoi_khoa_bieus
  # POST /thoi_khoa_bieus.json
  def create
    @thoi_khoa_bieu = ThoiKhoaBieu.new(params[:thoi_khoa_bieu])

    respond_to do |format|
      if @thoi_khoa_bieu.save
        format.html { redirect_to @thoi_khoa_bieu, notice: 'Thoi khoa bieu was successfully created.' }
        format.json { render json: @thoi_khoa_bieu, status: :created, location: @thoi_khoa_bieu }
      else
        format.html { render action: "new" }
        format.json { render json: @thoi_khoa_bieu.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /thoi_khoa_bieus/1
  # PUT /thoi_khoa_bieus/1.json
  def update
    @thoi_khoa_bieu = ThoiKhoaBieu.find(params[:id])

    respond_to do |format|
      if @thoi_khoa_bieu.update_attributes(params[:thoi_khoa_bieu])
        #puts @thoi_khoa_bieu.nhan_vien.inspect
        UserMailer.notice(NhanVien.find_by_MaNV(@thoi_khoa_bieu.MaNV_id)).deliver
        format.html { redirect_to @thoi_khoa_bieu, notice: 'Thoi khoa bieu was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @thoi_khoa_bieu.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /thoi_khoa_bieus/1
  # DELETE /thoi_khoa_bieus/1.json
  def destroy
    @thoi_khoa_bieu = ThoiKhoaBieu.find(params[:id])
    @thoi_khoa_bieu.destroy

    respond_to do |format|
      format.html { redirect_to thoi_khoa_bieus_url }
      format.json { head :no_content }
    end
  end

  def import
    if sign_in?
      @ts_import = ThoiKhoaBieu.new
      respond_to do |format|
        format.html
      end
    else
      render 'sessions/deny'
    end
  end

# Tạo đường dẫn để import
  def save
    if (ThoiKhoaBieu.import(params[:file]))
      redirect_to import_thoi_khoa_bieus_path, notice: t("flash.import_msg")
    else
      redirect_to import_thoi_khoa_bieus_path, notice: t("flash.import_error")
    end
  end

end


